package com.kktoolbox.model;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

/**
 * 文件详情对话框
 * 
 * <p>该对话框用于显示选中PDF文件的详细信息，
 * 并提供文件操作功能，如复制路径、打开文件夹等。</p>
 * 
 * <p>主要功能：</p>
 * <ul>
 *   <li>显示文件名、路径和大小</li>
 *   <li>复制文件路径到剪贴板</li>
 *   <li>在文件管理器中打开文件所在文件夹</li>
 *   <li>提供友好的用户界面</li>
 * </ul>
 * 
 * @author KK-Toolbox
 * @version 1.0.0
 * @since 2025-01-01
 */
public class FileDetailsDialog extends JDialog {
    
    /** 文件名 */
    private final String fileName;
    
    /** 文件完整路径 */
    private final String filePath;
    
    /** 文件大小字符串 */
    private final String fileSize;
    
    /**
     * 构造函数 - 创建文件详情对话框
     * 
     * @param parent 父窗口
     * @param fileName 文件名
     * @param filePath 文件完整路径
     * @param fileSize 文件大小字符串
     */
    public FileDetailsDialog(JFrame parent, String fileName, String filePath, String fileSize) {
        super(parent, "文件详情", true);
        
        this.fileName = fileName;
        this.filePath = filePath;
        this.fileSize = fileSize;
        
        initializeDialog();
        createUserInterface();
        setupEventListeners();
        
        // 居中显示
        setLocationRelativeTo(parent);
    }
    
    /**
     * 初始化对话框属性
     */
    private void initializeDialog() {
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setResizable(false);
        setSize(500, 300);
    }
    
    /**
     * 创建用户界面
     */
    private void createUserInterface() {
        setLayout(new BorderLayout());
        
        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
        
        // 创建信息面板
        JPanel infoPanel = createInfoPanel();
        mainPanel.add(infoPanel, BorderLayout.CENTER);
        
        // 创建按钮面板
        JPanel buttonPanel = createButtonPanel();
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
        
        add(mainPanel, BorderLayout.CENTER);
    }
    
    /**
     * 创建信息显示面板
     * 
     * @return 信息面板
     */
    private JPanel createInfoPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        
        // 设置通用约束
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.anchor = GridBagConstraints.WEST;
        
        // 文件名
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0;
        panel.add(new JLabel("文件名:"), gbc);
        
        gbc.gridx = 1;
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        JTextField fileNameField = new JTextField(fileName);
        fileNameField.setEditable(false);
        fileNameField.setBackground(Color.WHITE);
        panel.add(fileNameField, gbc);
        
        // 文件路径
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 0;
        gbc.fill = GridBagConstraints.NONE;
        panel.add(new JLabel("文件路径:"), gbc);
        
        gbc.gridx = 1;
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        JTextArea pathArea = new JTextArea(filePath);
        pathArea.setEditable(false);
        pathArea.setBackground(Color.WHITE);
        pathArea.setLineWrap(true);
        pathArea.setWrapStyleWord(true);
        pathArea.setRows(3);
        JScrollPane pathScrollPane = new JScrollPane(pathArea);
        pathScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        pathScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        panel.add(pathScrollPane, gbc);
        
        // 文件大小
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.weightx = 0;
        gbc.fill = GridBagConstraints.NONE;
        panel.add(new JLabel("文件大小:"), gbc);
        
        gbc.gridx = 1;
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        JTextField sizeField = new JTextField(fileSize);
        sizeField.setEditable(false);
        sizeField.setBackground(Color.WHITE);
        panel.add(sizeField, gbc);
        
        return panel;
    }
    
    /**
     * 创建按钮面板
     * 
     * @return 按钮面板
     */
    private JPanel createButtonPanel() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        
        // 复制路径按钮
        JButton copyPathButton = new JButton("复制路径");
        copyPathButton.setToolTipText("将文件路径复制到剪贴板");
        
        // 打开文件夹按钮
        JButton openFolderButton = new JButton("打开文件夹");
        openFolderButton.setToolTipText("在文件管理器中打开文件所在文件夹");
        
        // 关闭按钮
        JButton closeButton = new JButton("关闭");
        closeButton.setToolTipText("关闭此对话框");
        
        panel.add(copyPathButton);
        panel.add(openFolderButton);
        panel.add(closeButton);
        
        return panel;
    }
    
    /**
     * 设置事件监听器
     */
    private void setupEventListeners() {
        // 复制路径按钮事件
        Component[] components = ((JPanel) ((JPanel) getContentPane().getComponent(0)).getComponent(1)).getComponents();
        JButton copyPathButton = (JButton) components[0];
        JButton openFolderButton = (JButton) components[1];
        JButton closeButton = (JButton) components[2];
        
        copyPathButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                copyPathToClipboard();
            }
        });
        
        openFolderButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                openFileFolder();
            }
        });
        
        closeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
    }
    
    /**
     * 复制文件路径到剪贴板
     */
    private void copyPathToClipboard() {
        try {
            Toolkit.getDefaultToolkit()
                    .getSystemClipboard()
                    .setContents(new java.awt.datatransfer.StringSelection(filePath), null);
            
            JOptionPane.showMessageDialog(this, 
                "文件路径已复制到剪贴板", 
                "复制成功", 
                JOptionPane.INFORMATION_MESSAGE);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, 
                "复制路径失败: " + e.getMessage(), 
                "错误", 
                JOptionPane.ERROR_MESSAGE);
        }
    }
    
    /**
     * 在文件管理器中打开文件所在文件夹
     */
    private void openFileFolder() {
        try {
            File file = new File(filePath);
            File parentDir = file.getParentFile();
            
            if (parentDir != null && parentDir.exists()) {
                Desktop.getDesktop().open(parentDir);
            } else {
                JOptionPane.showMessageDialog(this, 
                    "文件夹不存在或无法访问", 
                    "错误", 
                    JOptionPane.ERROR_MESSAGE);
            }
        } catch (IOException e) {
            JOptionPane.showMessageDialog(this, 
                "无法打开文件夹: " + e.getMessage(), 
                "错误", 
                JOptionPane.ERROR_MESSAGE);
        } catch (UnsupportedOperationException e) {
            JOptionPane.showMessageDialog(this, 
                "当前系统不支持此操作", 
                "错误", 
                JOptionPane.ERROR_MESSAGE);
        }
    }
}